package com.henry.chatkit.conversation

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.henry.chatkit.R
import com.henry.chatkit.utils.appContext


class ConversationListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataSource = mutableListOf<ConversationInfo>()

    var onItemClickListener: ((view: View?, position: Int, conversation: ConversationInfo?) -> Unit)? = null
    var onItemLongClickListener: ((view: View?, position: Int, conversation: ConversationInfo?) -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(appContext())
        val holder: RecyclerView.ViewHolder?
        val view: View = inflater.inflate(R.layout.conversation_adapter, parent, false)
        holder = ConversationCommonHolder(view)
        (holder as ConversationBaseHolder).setAdapter(this)
        return holder
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val conversation = getItem(position)
        val baseHolder = holder as ConversationBaseHolder
        //设置点击和长按事件
        holder.itemView.setOnClickListener { view ->
            onItemClickListener?.invoke(view, position, conversation)
        }
        holder.itemView.setOnLongClickListener { view ->
            onItemLongClickListener?.invoke(view, position, conversation)
            true
        }
        baseHolder.layoutViews(conversation, position)
    }

    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
        if (holder is ConversationCommonHolder) {
            holder.conversationIconView.background = null
        }
    }

    override fun getItemCount(): Int {
        return mDataSource.size
    }

    fun getItem(position: Int): ConversationInfo? {
        return if (mDataSource.isEmpty()) null else mDataSource[position]
    }

    fun getData(): List<ConversationInfo> {
        return mDataSource
    }

    fun addItem(position: Int, info: ConversationInfo?) {
        if (info == null) {
            return
        }
        mDataSource.add(position, info)
        notifyItemInserted(position)
        notifyDataSetChanged()
    }

    fun removeItem(position: Int) {
        mDataSource.removeAt(position)
        notifyItemRemoved(position)
        notifyDataSetChanged()
    }

    fun addItems(list: List<ConversationInfo>) {
        mDataSource.addAll(list)
        notifyDataSetChanged()
    }

    fun reloadData(list: List<ConversationInfo>) {
        mDataSource.clear()
        mDataSource.addAll(list)
        notifyDataSetChanged()
    }

    fun notifyDataSourceChanged(info: String?) {
        for (i in mDataSource.indices) {
            if (TextUtils.equals(info, mDataSource[i].conversationId)) {
                notifyItemChanged(i)
                return
            }
        }
    }

}